package com.bdqn.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * EncodedFilter 过滤器统一设置字符编码
 *
 * @author LILIBO
 * @since 2024/5/24
 */
public class EncodedFilter implements Filter {

    private static String charsetEncoded = "UTF-8";

    /**
     * 初始化
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        charsetEncoded = filterConfig.getInitParameter("CharsetEncoded");
    }

    /**
     * 执行过滤操作
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        // 设置编码
        request.setCharacterEncoding(charsetEncoded);
        response.setCharacterEncoding(charsetEncoded);
        filterChain.doFilter(request, response);
    }

    /**
     * 销毁
     */
    public void destroy() {
    }

}
